#include "ThreadPool.hpp"
#include "Task.hpp"
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <memory>

class MyTask
:public tp::Task
{
public:
    MyTask(){
        //用线程ID和时间初始化种子，确保随机性
        srand(time(NULL) ^ pthread_self());
    }
    void process() override{
        /* srand(time(NULL)); */
        int num = rand() % 100;
        printf("num  = %d\n",num);
        sleep(1);
    }
};


int main()
{
    tp::ThreadPool threadpool(4,10);
    //01 启动线程池
    threadpool.start();

    //02 添加任务
    unique_ptr<tp::Task> task(new MyTask);
    int cnt = 20;
    while(cnt-- > 0){
        threadpool.addTask(task.get());
    }

    /* int cnt = 20; */
    /* while(cnt-- > 0){ */
    /*     unique_ptr<tp::Task> task(new MyTask); */
    /* //每次创建新任务，用release转移指针所有权 */
    /*     threadpool.addTask(task.release()); */
    /* } */

    //03 关闭线程池
    threadpool.stop();
    
    return 0;
}

